package team.pikachu.pikachuclient.message;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import team.pikachu.pikachuclient.R;
import team.pikachu.pikachuclient.beans.Trouble;
import team.pikachu.pikachuclient.utils.Constants;

import java.util.List;

public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

    private final List<Trouble> mValues;
    private OnItemClickListener mListener;

    public MyItemRecyclerViewAdapter(List<Trouble> items) {
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Trouble trouble = mValues.get(position);
        holder.mItem = trouble;

        holder.mTitleView.setText(trouble.getTruble());

        String color = "#000000";
        String state = "";
        switch (trouble.getState()){
            case Constants.STATUS_WAITING:
                state = "等待中";
                color = "#FF78909C";
                holder.mDateView.setText("上传时间："+trouble.getTime());
                break;
            case Constants.STATUS_DOING:
                state = "维修中";
                color = "#FFF2AF3A";
                holder.mDateView.setText("上传时间："+trouble.getTime());
                break;
            case Constants.STATUS_FINISHED:
                state = "已维修";
                color = "#FF279B5E";
                holder.mDateView.setText("完成时间："+trouble.getFinishtime());
                break;
        }
        holder.mStateView.setText(state);
        holder.mStateView.setTextColor(Color.parseColor(color));
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onItemClick(holder.mItem);
                }
            }
        });
        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mListener.onItemLongClick(holder.mItem);
                return true;
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(Trouble trouble);
        void onItemLongClick(Trouble trouble);
    }

    public void setOnItemClickListener(OnItemClickListener mListener){
        this.mListener = mListener;
    }


    @Override
    public int getItemCount() {
        return mValues.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        final View mView;
        final TextView mTitleView;
        final TextView mStateView;
        final TextView mDateView;
        Trouble mItem;

        ViewHolder(View view) {
            super(view);
            mView = view;
            mTitleView = (TextView) view.findViewById(R.id.title);
            mStateView = (TextView) view.findViewById(R.id.state);
            mDateView = (TextView) view.findViewById(R.id.date);
        }

    }

}
